package com.nwpu.bigdata.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Result;
import org.influxdb.dto.QueryResult.Series;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Service;

import com.nwpu.bigdata.dto.Cassandra;
import com.nwpu.bigdata.dto.Iotdb;
import com.nwpu.bigdata.service.IotdbService;
import com.nwpu.bigdata.util.impl.InfluxdbService;

/**
 *@version:2018年8月20日上午8:21:27
*/
/**
 * @author Administrator
 *
 */
@Service
@ImportResource(locations= {"classpath:/xmlConfig/spring/applicationContext-util.xml"})
public class IotdbServiceImpl implements IotdbService{

	/* (non-Javadoc)
	 * @see com.nwpu.bigdata.service.IotdbService#iotdbCol()
	 */
	
	@Value("${retentionPolicy.cassandra}")
	private String retentionPolicy;

	@Value("${influxdb.agentDB}")
	private String dbname;
	@Value("${influxdb.url}")
	private String url;
	@Value("${influxdb.user}")
	private String usrname;
	@Value("${influxdb.password}")
	private String password;
	@Override
	public List<Iotdb> iotdbCol() throws IOException {
		InfluxDB influxDB = InfluxDBFactory.connect(url, usrname, password);//建立influxdb连接
	    InfluxdbService service = new InfluxdbService(dbname,retentionPolicy,influxDB);
	    if(service.equals(null))
	    {
	    	
	    	return null;
	    }
	    
	    QueryResult queryResult = service.query("SELECT * FROM \"iotdb\" WHERE time > now() - 10m ORDER BY time DESC");//查询表中最近10分钟的数据
	    System.out.println(queryResult);
	   // List<QueryResult.Result> results = queryResult.getResults();//将查询结果放入list中
	    
	    if(queryResult.getResults() == null) {
			System.out.println("the result is empty, end query!");
			return null; 
		}
		
		List<Iotdb> lists = new ArrayList<Iotdb>();
		//遍历查询结果获取查询结果中有用的信息
		for(Result result : queryResult.getResults()){
			List<Series> series = result.getSeries();
			System.out.println("^^^^^^"+result.getSeries());
			for(Series serie : series) {
				
				List<List<Object>> values = serie.getValues();
				List<String> columns = serie.getColumns();
				
				System.out.println(serie.getName() + "  " + serie.getTags());
				
				System.out.println(columns);
				
				System.out.println(values);
				
				lists.addAll(getQueryData(columns, values));

				/*for(MetricInfo metricInfo : lists) {
					System.out.println(metricInfo);
				}
				*/
			}
			
		}
	   
	    return lists;
	}

	/* (non-Javadoc)
	 * @see com.nwpu.bigdata.service.IotdbService#getQueryData(java.util.List, java.util.List)
	 */
	@Override
	public List<Iotdb> getQueryData(List<String> columns, List<List<Object>> values) {
		  List<Iotdb> lists = new ArrayList<>();  
	      
		    for (List<Object> list : values) {  
		    	Iotdb info = new Iotdb();
		    	BeanWrapperImpl bean = new BeanWrapperImpl(info);
		        for(int i=0; i< list.size(); i++){  
//		          String propertyName = setColumns(columns.get(i));
		            String propertyName = columns.get(i);
		           
		            Object value = list.get(i); 
		            if(value ==null){
		            	bean.setPropertyValue(propertyName, 0); 
		            }
		            else{
		            bean.setPropertyValue(propertyName, value); 
		            }
		            
		        }  
		          
		        lists.add(info);  
		    }  
		      
		    return lists;  
		}
	}


